package com.yuanwushe.blue.onceandroid;

import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.yuanwushe.blue.adapter.MsgAdapter;
import com.yuanwushe.blue.models.Msg;

import java.util.ArrayList;
import java.util.List;

public class MsgActivity extends BaseActivity {

    public List<Msg> mMsgList = new ArrayList<>();
    public RecyclerView.Adapter mAdapter;
    public RecyclerView mRecyclerView;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_msg);
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null){
            actionBar.hide();
        }
        initMsg();
        init();
    }

    private void init() {
        mRecyclerView = findViewById(R.id.mes_recycler);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        mRecyclerView.setLayoutManager(linearLayoutManager);
        mAdapter = new MsgAdapter(mMsgList);
        mRecyclerView.setAdapter(mAdapter);
        Button sendButton = findViewById(R.id.send_button);
        sendButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                EditText sendMsg = findViewById(R.id.send_edit);
                String content = sendMsg.getText().toString();
                mMsgList.add(new Msg(content, Msg.TYPE_SENT));
                mAdapter.notifyItemChanged(mMsgList.size() - 1);
                mRecyclerView.scrollToPosition(mMsgList.size() - 1);
                sendMsg.setText("");
            }
        });
    }
    private void initMsg() {
        mMsgList.add(new Msg("第一条内容", Msg.TYPE_SENT));
        mMsgList.add(new Msg("第二条内容", Msg.TYPE_RECRIVED));
        mMsgList.add(new Msg("第三条内容", Msg.TYPE_SENT));
        mMsgList.add(new Msg("第四条内容", Msg.TYPE_RECRIVED));
    }
}
